package com.wlf.util;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author ex_hanyu
 *
 */
public class CommonFunctions {

	/**
	 * 判断对象是否为空
	 * @param object
	 * @return
	 */
	public static boolean  isEmpty(Object object){

		if(object==null || object.toString().trim().length()==0)
			return true;
		else
			return false;

	}

	/**
	 * 判断集合是否为空
	 * @param object
	 * @return
	 */
	public static boolean  isEmpty(List object){

		if(object==null || object.size()==0)
			return true;
		else
			return false;

	}

	/**
	 * 获取年份
	 * @return
	 */
	public static String getYear(){

		Calendar date=Calendar.getInstance();
		return String.valueOf(date.get(Calendar.YEAR));

	}

	/**
	 * 获取月份
	 * @return
	 */
	public static String getMonth(){

		Calendar date=Calendar.getInstance();
		int mon = date.get(Calendar.MONTH)+1;
		if( mon < 10 )
			return "0"+mon;
		else
			return String.valueOf(mon);

	}

	/**
	 * 获取日期
	 * @return
	 */
	public static String getDay(){

		Calendar date=Calendar.getInstance();
		int day = date.get(Calendar.DAY_OF_MONTH);
		if( day < 10 )
			return "0"+day;
		else
			return String.valueOf(day);

	}

	/**
	 * 判断字符是否存在
	 * @param cons
	 * @param variy
	 * @return
	 */
	public static boolean isExists(String cons,String variy){
		if(!isEmpty(cons) && !isEmpty(variy)){
			String [] conStrings=cons.split(",");
			if(!isEmpty(conStrings)){
				for(int i=0;i<conStrings.length;i++){
					if(variy.equals(conStrings[i]))
					{
						return true;
					}
				}
			}
		}
		return false;

	}


	/**
     * 匹配是否为数字
     * @param str 可能为中文，也可能是-19162431.1254，不使用BigDecimal的话，变成-1.91624311254E7
     * @return
     * @author yutao
     * @date 2016年11月14日下午7:41:22
     */
    public static boolean isNumeric(String str) {
        // 该正则表达式可以匹配所有的数字 包括负数
        Pattern pattern = Pattern.compile("-?[0-9]+(\\.[0-9]+)?");
        String bigStr;
        try {
            bigStr = new BigDecimal(str).toString();
        } catch (Exception e) {
            return false;//异常 说明包含非数字。
        }

        Matcher isNum = pattern.matcher(bigStr); // matcher是全匹配
        if (!isNum.matches()) {
            return false;
        }
        return true;
    }

    /**
	 * 将Map中的key由下划线转换为驼峰
	 *
	 * @param map
	 * @return
	 */
	public static Map<String, Object> formatHumpName(Map<String, Object> map) {
		if(null == map){
			return null;
		}
		Map<String, Object> newMap = new HashMap<String, Object>();
		Iterator<Map.Entry<String, Object>> it = map.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry<String, Object> entry = it.next();
			String key = entry.getKey();
			String newKey = toFormatCol(key);
			newMap.put(newKey,entry.getValue());
		}
		return newMap;
	}

	public static String toFormatCol(String colName) {
		StringBuilder sb = new StringBuilder();
		String[] str = colName.toLowerCase().split("_");
		int i = 0;
		for (String s : str) {
			if (s.length() == 1) {
				s = s.toUpperCase();
			}
			i++;
			if (i == 1) {
				sb.append(s);
				continue;
			}
			if (s.length() > 0) {
				sb.append(s.substring(0, 1).toUpperCase());
				sb.append(s.substring(1));
			}
		}
		return sb.toString();
	}

	/**
	 * 将List中map的key值命名方式格式化为驼峰
	 *
	 * @param
	 * @return
	 */
	public static List<Map<String, Object>> formatHumpNameForList(List<Map<String, Object>> list) {
		List<Map<String, Object>> newList = new ArrayList<Map<String, Object>>();
		for (Map<String, Object> o : list) {
			newList.add(formatHumpName(o));
		}
		return newList;
	}

	public static Object getCloneObject(Object bean) {
	    Object cloneBean = null;
	    try {
	      ByteArrayOutputStream byout = new ByteArrayOutputStream();
	      ObjectOutputStream obj = new ObjectOutputStream(byout);
	      obj.writeObject(bean);
	      ByteArrayInputStream byin = new ByteArrayInputStream(byout.toByteArray());
	      ObjectInputStream ins = new ObjectInputStream(byin);
	      cloneBean = (Object) ins.readObject();

	    } catch (Exception ex) {
	      ex.printStackTrace();
	    }
	    return cloneBean;

	  }

}
